<?php include '../../checkPCandVPC.php'; ?>
<?php include 'top.html'; ?>
<?php include 'notifier.php'; ?>
<?php include '../../authorization_check.php'; ?>

  <?php
  require_once '../../commonutils/util/PageTemplate.php';
  require_once '../../commonutils/db/DBConnectionManager.php';


  //global variables
  $totalPayment = 0;
  $count = 0;
  $dcount = 0;
  $totalCredit = 0;
  $totalPayment = 0;
  $depSC;
  $bosrow = 0;
  //$dep; 

  function listNonProgStudentsForm($webPage){
  $webPage->addContent("<form name='listNonProgStudentsforMSForm' method='post' action=''>");				
  }

  //FIND THE NUMBER OF RECORDS AND NUMBER OF DIFFERENT DEPARTMENTS GLOBALY



  $result = DBConnectionManager::query("SELECT DISTINCT
    enrollment.studentID,
    enrollment.semesterID,
    enrollment.sectionID,
    course.courseName,
    course.courseCode,
    student.lastName,
    student.firstName,
    section.semesterID,
    section.sectionID,
    section.courseCode,
    student.programCode,
    course.credit,
    program.longName,
    program.programCode,
    student.studentID
  FROM
    enrollment,
    course,
    student,
    section,
    program
  WHERE
    (enrollment.studentID = student.studentID) AND
    (enrollment.semesterID = section.semesterID) AND
    (enrollment.sectionID = section.sectionID) AND
    (section.courseCode = course.courseCode) AND
    (student.programCode = program.programCode) AND
    (student.programCode <> '9030') AND
    (section.courseCode LIKE '9030%')
  GROUP BY
    enrollment.studentID,
    enrollment.semesterID,
    enrollment.sectionID,
    section.courseCode
  ORDER BY
    student.programCode
  ");
		
		//-----------------------------------------------------------
		//ERRRRRRRRRRRRRRRRRRRROOOOOOOOOOOOOOOOOOOOOOR
		//print ("Error: (" . mysql_errno() . ") " . mysql_error());
    //-----------------------------------------------------------
    
    //$row = mysql_fetch_assoc($result);
      $dep[$dcount] = 'xx';
      
      //$names = $row['firstName'];
      //echo "$names";
      $depSC[$dcount] = 1;
      
      
      while($row = mysql_fetch_assoc($result)){
      //$names2 = $row['firstName'];
      if( $row['programCode'] != $dep[$dcount] ){
      		$depN[$dcount] = $row['longName'];
      		$dcount++;
            $depSC[$dcount] = 0;
            //echo "depSC[".$dcount."]=".$depSC[$dcount]."<br>";
            $dep[$dcount] = $row['programCode'];
            $depN[$dcount] = $row['longName'];
            
            }			
        $depSC[$dcount]++;
        //echo "depSC[".$dcount."]=".$depSC[$dcount]."<br>";
      }
    /*echo "onceki dep: ";
    echo "$dep[0]";
    echo "ustteki count: ";
    echo "$count"+1;
    echo "ustteki dcount: ";
    echo "$dcount"+1;*/

  function listStudents($webPage, $loopNo){
    
    global $count, $dcount, $dep, $depN, $totalCredit, $totalPayment,$bosrow, $depSC ;
    $totalCredit = 0;
    $totalPayment = 0;
    //echo "loopNo:"."$loopNo";
    
    //echo "sonraki dep: ";
    //echo "$dep[0]";
      
    $webPage->addContent("<form name='listNonProgStudentsforMSForm' method='post'>");
    $webPage->addContent("<table align = 'center' border=1 name='listNonProgStudentsforMSTable'>");
    $webPage->addContent("<tr>");
      $webPage->addContent("<th>Student ID</th>");
      $webPage->addContent("<th>Name</th>");
      $webPage->addContent("<th>Surname</th>");
      $webPage->addContent("<th>Department</th>");
      $webPage->addContent("<th>Course Code</th>");
      $webPage->addContent("<th>Course Name</th>");
      $webPage->addContent("<th>Course Credit</th>");
      $webPage->addContent("<th>Total Payment (YTL)</th>");
    $webPage->addContent("</tr>");
    
    //echo "dep[loopNo]: ";
    //echo "$dep[$loopNo]";
    
      $result = DBConnectionManager::query("SELECT DISTINCT
    enrollment.studentID,
    enrollment.semesterID,
    enrollment.sectionID,
    course.courseName,
    course.courseCode,
    student.lastName,
    student.firstName,
    section.semesterID,
    section.sectionID,
    section.courseCode,
    student.programCode,
    course.credit,
    program.longName,
    program.programCode,
    student.studentID
  FROM
    enrollment,
    course,
    student,
    section,
    program
  WHERE
    (enrollment.studentID = student.studentID) AND
    (enrollment.semesterID = section.semesterID) AND
    (enrollment.sectionID = section.sectionID) AND
    (student.programCode = program.programCode) AND
    (section.courseCode = course.courseCode) AND
    (student.programCode <> '9030') AND
    (section.courseCode LIKE '9030%')
  GROUP BY
    enrollment.studentID,
    enrollment.semesterID,
    enrollment.sectionID,
    section.courseCode
  ORDER BY
    student.programCode
  "); 
    //echo "for oncesi: ";
    //echo "$count"+1;
    $bosrow = 0;
    //echo "loopNo:".$loopNo;
    for($l=0; $l < $loopNo ; $l++){
    //echo "xxxxx".$depSC[$l]."<br>";
    $bosrow = $bosrow + $depSC[$l];
    
    //for($k =0; $k <= $depSC[$l]; $k++){
    //  $bosrow ++;
    //  echo $k;
    //  echo $bosrow;
    //}
    }
    //echo "bosRow Sayisi for:".$loopNo.": ".$bosrow."<br>";
    $i = 0;
    $j = 0;
    while($row = mysql_fetch_assoc($result))
    {
      if($i < $bosrow)
        {
          //echo "i:".$i."<br>";
        }
        
        else{
        if($j < $depSC[$loopNo]){
        $studentID = $row['studentID'];
        //echo "$studentID"." "; 
        $firstName = $row['firstName']; 
        //echo "$firstName"." "; 
        $lastName = $row['lastName']; 
        //echo "$lastName"." "; 
        $department = $row['longName'];
        //echo "$department"." "; 
        $courseCode = $row['courseCode'];
        //echo "$courseCode"." "; 
        $courseName = $row['courseName'];
        //echo "$courseName"." "; 
        $credit = $row['credit'];
        //echo "$credit"." "; 
        $totalCredit +=$credit;
        //echo "$totalCredit"." "; 
        $totalStudentPayment = 300 * $credit;
        //echo "$totalStudentPayment"." "; 
        $totalPayment += $totalStudentPayment;
        //echo "$totalPayment"."<br>"; 
        $webPage->addContent("<tr>");
          $webPage->addContent("<td>$studentID</td>");
          $webPage->addContent("<td>$firstName</td>");
          $webPage->addContent("<td>$lastName</td>");
          $webPage->addContent("<td>$department</td>");
          $webPage->addContent("<td>$courseCode</td>");
          $webPage->addContent("<td>$courseName</td>");
          $webPage->addContent("<td align = 'center'>$credit</td>");
          $webPage->addContent("<td align = 'center'>$totalStudentPayment</td>");
          //$webPage->addContent("<td>$totalPaymentForACourse</td>");	
        $webPage->addContent("</tr>");
        //$count++;
      
    
    
    
    $j ++;
    }
    else
    {
    break;
    }
    
    }
    $i ++;
  }
  
  
  $webPage->addContent("<tr><td colspan = 6/></td><th align= left><u>Total Payment</u></th><th align=center>$totalPayment</th>");
    $webPage->addContent("<tr><td colspan = 6/></td><th align= left><u>Total Credit</u></th><th align=center>$totalCredit</th>");
    $webPage->addContent("</tr>");
    $webPage->addContent("</table>");
    $webPage->addContent("<table align = 'center'><tr>");
    $webPage->addContent("<td>");
    $webPage->addContent("</td>");
    $webPage->addContent("<td>");
    $webPage->addContent("</td>");
    $webPage->addContent("</tr></table></form>");
    
    
    
    /*for($i =0 ; $i <= $depSC[$loopNo] ; $i++){
        $row = mysql_fetch_assoc($result);
        if($i <= $bosrow)
        {
        $studentID = $row['studentID'];
        //echo "$studentID"." "; 
        $firstName = $row['firstName']; 
        //echo "$firstName"." "; 
        $lastName = $row['lastName']; 
        //echo "$lastName"." "; 
        $department = $row['longName'];
        //echo "$department"." "; 
        $courseCode = $row['courseCode'];
        //echo "$courseCode"." "; 
        $courseName = $row['courseName'];
        //echo "$courseName"." "; 
        $credit = $row['credit'];
        //echo "$credit"." "; 
        $totalCredit +=$credit;
        //echo "$totalCredit"." "; 
        $totalStudentPayment = 300 * $credit;
        //echo "$totalStudentPayment"." "; 
        $totalPayment += $totalStudentPayment;
        }
        
        else{
        
        $studentID = $row['studentID'];
        //echo "$studentID"." "; 
        $firstName = $row['firstName']; 
        //echo "$firstName"." "; 
        $lastName = $row['lastName']; 
        //echo "$lastName"." "; 
        $department = $row['longName'];
        //echo "$department"." "; 
        $courseCode = $row['courseCode'];
        //echo "$courseCode"." "; 
        $courseName = $row['courseName'];
        //echo "$courseName"." "; 
        $credit = $row['credit'];
        //echo "$credit"." "; 
        $totalCredit +=$credit;
        //echo "$totalCredit"." "; 
        $totalStudentPayment = 300 * $credit;
        //echo "$totalStudentPayment"." "; 
        $totalPayment += $totalStudentPayment;
        //echo "$totalPayment"."<br>"; 
        $webPage->addContent("<tr>");
          $webPage->addContent("<td>$studentID</td>");
          $webPage->addContent("<td>$firstName</td>");
          $webPage->addContent("<td>$lastName</td>");
          $webPage->addContent("<td>$department</td>");
          $webPage->addContent("<td>$courseCode</td>");
          $webPage->addContent("<td>$courseName</td>");
          $webPage->addContent("<td align = 'center'>$credit</td>");
          $webPage->addContent("<td align = 'center'>$totalStudentPayment</td>");
          //$webPage->addContent("<td>$totalPaymentForACourse</td>");	
        $webPage->addContent("</tr>");
        //$count++;
      
    
    $webPage->addContent("<tr><td colspan = 6/></td><th align= left><u>Total Payment</u></th><th align=center>$totalPayment</th>");
    $webPage->addContent("<tr><td colspan = 6/></td><th align= left><u>Total Credit</u></th><th align=center>$totalCredit</th>");
    $webPage->addContent("</tr>");
    $webPage->addContent("</table>");
    $webPage->addContent("<table align = 'center'><tr>");
    $webPage->addContent("<td>");
    $webPage->addContent("</td>");
    $webPage->addContent("<td>");
    $webPage->addContent("</td>");
    $webPage->addContent("</tr></table></form>");
    }
    }*/
  }


  //BURASI SAYFANIN BASLIGININ VE FOOTER'ININ OLUSTURULDUGU YER

  $webPage = new Page();


  //addJavaScripts($webPage);

  $webPage->addHeader('Non-MS Students Taking Modelling and Simulations (MS) Courses Details');

  //$lc = $dcount + 1;
  //echo "en alttaki lc: ";
  //echo "$lc";
    
  listNonProgStudentsForm($webPage);

    $dc = $dcount;
    /*echo "en alttaki dcount: ";
    echo "$dc";
    echo "<br>";
    echo $bosrow;*/

  if($dc == 0){
      $webPage->addCenterContent("!!! THERE ISN'T ANY NON-MS STUDENT TAKING MS COURSES !!!");
    }else{
  for($j = 0; $j < $dc; $j++){
      //echo "j: "."$j";
      $webPage->addCenterContent("Students from $depN[$j] Department");
      listStudents($webPage,$j);		
      }
    }
  $webPage->addFooter(date('Y'), 'IIPIS');
  echo $webPage->get();
  include 'bottom.php';
  ?>